package com.fit.pages;


import java.util.LinkedList;

import org.apache.tapestry5.Block;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.annotations.SetupRender;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.fit.beans.CarBean;
import com.fit.enums.AccountRole;
import com.fit.interfaces.BorrowI;
import com.fit.interfaces.CarI;
import com.fit.interfaces.ServiceI;
import com.fit.interfaces.User;
import com.fit.service.CarsI;
import com.fit.services.AppModule;

public class EntryCar {

	
	@SessionState
	private User user;
	
	private boolean userExists;

	public User getUser() {
		return user;
	}

	Object onActivate(){
		if(!userExists) return Login.class;
		if(user.getRole() == AccountRole.USER) return Index.class;
		return null;
	}
	

	@Persist
	@Property
	private CarI currentCar;
	
	@Property
	@Inject
	private Block edit;
	
	@Property
	@Inject
	private Block congratulations;
	
	@Inject
	private CarsI cars;
	
	@SetupRender
	public void createObject(){
		currentCar = (CarI) AppModule.context.getBean("car");
		currentCar.setBorrows(new LinkedList<BorrowI>());
		currentCar.setServices(new LinkedList<ServiceI>());
	}
	
	Object onSuccess(){
			currentCar.setServiceDone(false);
		  cars.save(currentCar);
		return congratulations;
	}
}
